package com.ca.ridersclubapplication.view.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.R;

public class MainFooterButton extends ConstraintLayout {

    private Drawable primaryIcon;
    private Drawable secondIcon;
    private Drawable otherIcon;
    private int type;
    private String text;

    public ImageView iconView;
    public TextView textView;

    public MainFooterButton(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public MainFooterButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public MainFooterButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.main_footer_button_layout, this);

        iconView = view.findViewById(R.id.iv);
        textView = view.findViewById(R.id.tv);

        if (attrs != null) {

            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MainFooterButton, 0, 0);

            primaryIcon = array.getDrawable(R.styleable.MainFooterButton_mfb_primary);
            secondIcon = array.getDrawable(R.styleable.MainFooterButton_mfb_second);
            type = array.getInteger(R.styleable.MainFooterButton_mfb_type, 1);
            text = array.getString(R.styleable.MainFooterButton_mfb_text);

            array.recycle();
        }

        setPrimaryIcon(primaryIcon);
        setSecondIcon(secondIcon);
        setType(type);
        setText(text);

        setOnClickListener(v -> {});
    }

    public Drawable getPrimaryIcon() {
        return primaryIcon;
    }

    public void setPrimaryIcon(Drawable primaryIcon) {
        this.primaryIcon = primaryIcon;
        setType(type);
    }

    public Drawable getSecondIcon() {
        return secondIcon;
    }

    public void setSecondIcon(Drawable secondIcon) {
        this.secondIcon = secondIcon;
        setType(type);
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
        if (this.type == 0) {
            this.iconView.setImageDrawable(primaryIcon);
            this.textView.setTextColor(getResources().getColor(R.color.primary));
        }
        else {
            this.type = 1;
            this.iconView.setImageDrawable(secondIcon);
            this.textView.setTextColor(getResources().getColor(R.color.secondary_text));
        }
    }

    public void primary() {
        setType(0);
    }

    public void second() {
        setType(1);;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text == null ? "" : text;
        this.textView.setText(this.text);
    }

    public Drawable getOtherIcon() {
        return otherIcon;
    }

    public void setOtherIcon(Drawable otherIcon) {
        this.otherIcon = otherIcon;
    }

    public void showOtherIcon(boolean show) {
        if (show) {
            iconView.setImageDrawable(otherIcon);
        }
        else {
            setType(type);
        }
    }
}
